<template>
  <div class="dpro-checkbox-bigBox">
    <span class="dpro-checkbox-box" v-for="(item, index) in checkedGroup" :class="{
      'is-disabled': item.disabled,
      'is-checked': value.includes(item.value),
      'is-indeterminate': indeterminate
    }" :key="index" @click.stop="chooseSCheckbox(item, index)">
      <span class="dpro-checkbox__inner"></span>
      <input type="checkbox" :value="value.includes(item.value)" id="dpro-checkbox" class="dpro-checkbox">
      <slot></slot>
      <label class="dpro-label" style="">{{ item.label }}</label>
    </span>
  </div>
</template>

<script>
export default {
  name: "DproCheck",
  data() {
    return {}
  },
  props: {
    checkedGroup: {
      type: Array,
      default: []
    },
    disabled: Boolean,
    value: {
      type: Array,
      default: []
    },
    indeterminate: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    chooseSCheckbox(val, index) {
      if (val.disabled) {
        return false;
      }
      let checked = this.value.includes(val.value),
        newArr = checked ? this.value.filter(item => item !== val.value) : this.value.concat([val.value]);
      this.$emit('change', newArr);
      this.$emit('input', newArr);
    }
  }
}
</script>

<style lang="less">
@import url(../../styles/variables.less);

.dpro-checkbox__inner {
  border: 1px solid #dcdfe6;
  width: 14px;
  height: 14px;
  background-color: #fff;
  position: relative;
  cursor: pointer;
  display: inline-block;
  box-sizing: border-box;
  top: 2px;
}

.dpro-checkbox__inner:after {
  box-sizing: content-box;
  content: "";
  border: 1px solid #fff;
  border-left: 0;
  border-top: 0;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(1);
  width: 3px;
  // transition: transform .15s ease-in .05s;
  transform-origin: center;

}

.dpro-checkbox-box.is-checked .dpro-checkbox__inner {
  border-color: @primary-color;
  background: @primary-color;
}

.dpro-label {
  cursor: pointer;
  margin-left: 4px;
  margin-right: 6px;
}

.dpro-checkbox {
  outline: none;
  position: absolute;
  z-index: -1;
  top: 4px;
  left: 1px;
  right: 0;
  bottom: 0;
  margin: 0;
  opacity: 0;
}

.dpro-checkbox-box {
  position: relative;
  cursor: pointer;
  min-width: 90px;
  padding-bottom: 10px;
}

.dpro-checkbox-box.is-disabled {
  cursor: not-allowed;
}

.dpro-checkbox-box.is-disabled .dpro-checkbox__inner {
  background: #EEEEEE;
  cursor: not-allowed;
}

.dpro-checkbox-box.is-disabled label {
  color: #868686;
  cursor: not-allowed;
}

.dpro-checkbox-box.is-disabled .dpro-checkbox__inner:after {
  background: #EEEEEE;
  border-color: #EEEEEE;
  cursor: not-allowed;
}

.dpro-checkbox-box.is-disabled.is-checked label {
  color: #868686
}

.dpro-checkbox-box.is-disabled.is-checked .dpro-checkbox__inner {
  border-color: #DCDFE6;
}

.dpro-checkbox-box.is-disabled.is-checked .dpro-checkbox__inner:after {
  border-color: #A0A0A0;
}
.dpro-checkbox-box.is-indeterminate .dpro-checkbox__inner:after {
  width: 7px;
  height: 7px;
  border: none;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate3d(-50%, -50%, 0);
  transition: none;
}
</style>